package com.formula.datastructure.exam.list;

import com.formula.datastructure.util.DataUtil;

public class LinkedList {
    private LinkedNode head;
    private LinkedNode tail;

    public void init() {
        head = new LinkedNode(null);
        tail = head;
    }

    public boolean isEmpty() {
        return head.next == null;
    }

    public void insertFromHead(Integer data) {
        LinkedNode newNode = new LinkedNode(data);
        if (head.next == null) {
            tail.next = newNode;
        } else {
            newNode.next = head.next;
        }
        head.next = newNode;
    }

    public void insertFromTail(Integer data) {
        LinkedNode newNode = new LinkedNode(data);
        if (head.next == null) {
            head.next = newNode;
        } else {
            tail.next.next = newNode;
        }
        tail.next = newNode;
    }

    public static void main(String[] args) {
        LinkedList list = new LinkedList();
        list.init();

        Integer[] array1 = DataUtil.genUniqueArray(10, 200);
        Integer[] array2 = DataUtil.genUniqueArray(10, 999);
        DataUtil.printIndex(10);
        DataUtil.printArray(array1);
        DataUtil.printArray(array2);

        for (int i = 0; i < 10; i++) {
            list.insertFromHead(array1[i]);
        }

        for (int i = 0; i < 10; i++) {
            //list.insertFromTail(array2[i]);
        }

        System.out.println();
    }
}
